-module(app).


-export([set_file/1,test/0,write/0]).

set_file(Module) ->
	put(module,Module),	
	ok.
write() ->
	write_erlang().

test() ->
	set_file(appc_agentCtr),
	write_erlang().

write_erlang() ->
	case get(module) of
		module -> {error,no_module};
		Module -> 
			write_erlang(Module)
	end.


write_erlang(Module) ->
	Name = Module:get_name(),
	DataDes = Module:get_des(),
	create_config_file(Name,DataDes),
	create_bat_file(Name,DataDes),
%% 	create_unix_file(Name,DataDes),
	create_app_config_file(Name,DataDes),
	create_app_rel_file(Name,DataDes),
	create_app_file(Name,DataDes),
	case create_sup_file(Name,erlang:list_to_atom(erlang:atom_to_list(Name) ++ "_sup"),DataDes) of
		{WorkList,SupList} ->
			FunMakeWorker = fun({WorkerName,WorkerMoudle}) -> create_worker_file(Module,WorkerName,WorkerMoudle) end,
			lists:foreach(FunMakeWorker, WorkList),
			FunMakeSuper = fun({SuperName,SuperMoudle}) -> create_super_file(Module,SuperName,SuperMoudle) end,
			lists:foreach(FunMakeSuper, SupList);
		_ -> ok
	end.
  
create_config_file(Name,DataDes) ->
	Create = erlang:atom_to_list(Name) ++ "_cnf",
	case file:open(Create ++ ".config", [write]) of
		{ok, File} ->
			file:write(File, "[\r\n"),
			{config,ConfigStr} = lists:keyfind(config, 1, DataDes),
			file:write(File, "\t{" ++ erlang:atom_to_list(Name) ++ ", [" ++ ConfigStr ++ "]}\r\n"),
			file:write(File, "].\r\n"),
			file:close(File);			
		_ -> skip
	end.

create_bat_file(Name,_DataDes) ->
	Create = "start_" ++ erlang:atom_to_list(Name),
	case file:open(Create ++ ".bat", [write]) of
		{ok, File} ->
			file:write(File, "\r\n"),
			file:write(File, "cd /d %~dp0\r\n"),
%% 			file:write(File, "erl.exe  -pa E:\ServerWork\PhoneRes\ebin E:\ServerWork\phoneSvr\ebin -boot " ++ erlang:atom_to_list(Name) ++ " -config " ++ erlang:atom_to_list(Name) ++ "_cnf" ++ "\r\n"),
			file:write(File, "erl.exe -boot " ++ erlang:atom_to_list(Name) ++ " -config " ++ erlang:atom_to_list(Name) ++ "_cnf" ++ "\r\n"),
			file:close(File);			
		_ -> skip
	end.

%% create_unix_file(Name,DataDes) ->
%% 	Create = "start_" ++ erlang:atom_to_list(Name),
%% 	case file:open(Create ++ ".bat", [write]) of
%% 		{ok, File} ->
%% 			file:close(File);			
%% 		_ -> skip
%% 	end.

create_app_config_file(Name,_DataDes) ->
	Create = erlang:atom_to_list(Name),
	case file:open(Create ++ ".app", [write]) of
		{ok, File} ->
			file:write(File, "{application, " ++ erlang:atom_to_list(Name) ++ ",\r\n"),
			file:write(File, "[{description, \"" ++ erlang:atom_to_list(Name) ++ " wrapper\"},\r\n"),
			file:write(File, "\t{vsn, \"0.0.1\"},\r\n"),
			file:write(File, "\t{modules, [ " ++ erlang:atom_to_list(Name) ++ "_app, " ++ erlang:atom_to_list(Name) ++ "_sup]},\r\n"),
			file:write(File, "\t{registered,[" ++ erlang:atom_to_list(Name) ++ "]},\r\n"),
			file:write(File, "\t{applications, [kernel,stdlib]},\r\n"),
			file:write(File, "\t{mod, {" ++ erlang:atom_to_list(Name) ++ "_app,[]}},\r\n"),
			file:write(File, "\t{start_phases, []}\r\n"),
			file:write(File, "]}.\r\n"),
			file:close(File);			
		_ -> skip
	end.

create_app_rel_file(Name,DataDes) ->
	Create = erlang:atom_to_list(Name),
	case file:open(Create ++ ".rel", [write]) of
		{ok, File} ->
			{rel,ErtsStr,AppList} = lists:keyfind(rel, 1, DataDes),
			file:write(File, "{release, {\"game\", \"R1\"}, " ++ ErtsStr ++ ",\r\n"),
			file:write(File, "\t[\r\n"),
			Fun = fun(AppStr) -> file:write(File, "\t" ++ AppStr ++ ",\r\n") end,
			lists:foreach(Fun, AppList),
			file:write(File, "\t{" ++ erlang:atom_to_list(Name) ++ ", \"0.0.1\"}\r\n"),
			file:write(File, "\t]\r\n"),
			file:write(File, "}.\r\n"),
			file:close(File);			
		_ -> skip
	end.

create_app_file(Name,DataDes) ->
	Create = erlang:atom_to_list(Name) ++ "_app",
	case file:open(Create ++ ".erl", [write]) of
		{ok, File} ->
			file:write(File, "-module(" ++ erlang:atom_to_list(Name) ++ "_app).\r\n"),
			file:write(File, "-behaviour(application).\r\n"),
			file:write(File, "-export([start/2, stop/1,do_start/0]).\r\n"),
			file:write(File, "\r\n"),
			{dbm,BDbm} = lists:keyfind(dbm, 1, DataDes),
			{table,Table} = lists:keyfind(table, 1, DataDes),
			file:write(File, "start(_StartType, _StartArgs) -> lib_app:app_start(" ++ erlang:atom_to_list(BDbm) ++ ",\"" ++ Table ++ "\",?MODULE).\r\n"),
			file:write(File, "do_start() ->\r\n"),
			file:write(File, "\tcase " ++ erlang:atom_to_list(Name) ++ "_sup:start_link() of \r\n"),
			file:write(File, "\t\t{ok, Pid} ->          {ok, Pid};\r\n"),
			file:write(File, "\t\tOther ->          {error, Other}    \r\n"),
			file:write(File, "\tend.\r\n"),
			file:write(File, "stop(_State) ->\r\n"),
			file:write(File, "\tok.\r\n"),
			file:close(File);			
		_ -> skip
	end.

create_sup_file(_Name,Module,DataDes) ->
	Create = erlang:atom_to_list(Module),
	case file:open(Create ++ ".erl", [write]) of
		{ok, File} ->
			file:write(File, "-module(" ++ erlang:atom_to_list(Module) ++ ").\r\n"),
			file:write(File, "-behaviour(supervisor).\r\n"),
			file:write(File, "-export([start_link/0,init/1,start/0]).\r\n"),
			file:write(File, "\r\n"),
			file:write(File, "start_link() ->supervisor:start_link({local, ?MODULE}, ?MODULE, []).\r\n"),
			file:write(File, "start() ->?MODULE:start_link().\r\n"),
			{sup_des,SupDes} = lists:keyfind(sup_des, 1, DataDes),
			{{restart_strategy,RestartStrategyStr},{sup_list,SupList}} = SupDes,
			file:write(File, "init([]) ->\r\n"),
			Fun = fun({SupItemName,SupItemMoudle,RestartStr,ShutdownStr,Type},{Names,Workers,Sups}) ->
						  case Type of
							  worker ->
								  file:write(File, "\tItem_" ++ erlang:atom_to_list(SupItemName) ++ 
												 " = {" ++ erlang:atom_to_list(SupItemName) ++ ", {" ++ erlang:atom_to_list(SupItemMoudle) ++ 
												 ", start_link, []}, " ++ RestartStr ++ ", " ++ ShutdownStr ++ ", worker, [" ++ erlang:atom_to_list(SupItemMoudle) ++ 
												 "]},\r\n"),
								  {Names ++ ",Item_" ++ erlang:atom_to_list(SupItemName),Workers ++ [{SupItemName,SupItemMoudle}],Sups};								
							  supervisor ->
								  file:write(File, "\tItem_" ++ erlang:atom_to_list(SupItemName) ++ 
												 " = {" ++ erlang:atom_to_list(SupItemName) ++ ", {" ++ erlang:atom_to_list(SupItemMoudle) ++ 
												 ", start_link, []}, " ++ RestartStr ++ ", " ++ ShutdownStr ++ ", supervisor, [" ++ erlang:atom_to_list(SupItemMoudle) ++ 
												 "]},\r\n"),
								  {Names ++ ",Item_" ++ erlang:atom_to_list(SupItemName),Workers,Sups ++ [{SupItemName,SupItemMoudle}]}
						  end
			end,
			{GetNames,GetWorkers,GetSups} = lists:foldl(Fun, {"",[],[]}, SupList),
			GetNames1 = string:sub_string(GetNames, 2),
			file:write(File, "\t{ok, {" ++ RestartStrategyStr ++ ", [" ++ GetNames1 ++ "]}}.\r\n"),
			file:close(File),
			{GetWorkers,GetSups};
		_ -> skip
	end.

create_worker_file(Module,WorkerName,WorkerMoudle) ->
	DataDes = Module:get_des(WorkerName),
	{worker_des,WorkerDes} = lists:keyfind(worker_des, 1, DataDes),
	{global,Global} = lists:keyfind(global, 1, WorkerDes),
	
	Create = erlang:atom_to_list(WorkerMoudle),
	case file:open(Create ++ ".erl", [write]) of
		{ok, File} ->
			file:write(File, "-module(" ++ erlang:atom_to_list(WorkerMoudle) ++ ").\r\n"),
			file:write(File, "-behaviour(gen_server).\r\n"),
			file:write(File, "-export([start_link/0, stop/0, init/1, terminate/2, code_change/3, handle_cast/2, handle_info/2, handle_call/3]).\r\n"),
			file:write(File, "\r\n"),
			
			case Global of
				true -> 
					file:write(File, "start_link() -> gen_server:start_link({global, " ++ erlang:atom_to_list(WorkerName) ++ "}, ?MODULE, [], []).\r\n"),
					file:write(File, "stop() ->gen_server:cast({global, " ++ erlang:atom_to_list(WorkerName) ++ "}, stop).\r\n");
				_ -> 
					file:write(File, "start_link() -> gen_server:start_link(?MODULE, [], []).\r\n"),
					file:write(File, "stop() ->gen_server:cast(?MODULE, stop).\r\n")
			end,
			file:write(File, "init([]) -> {ok,{}}.\r\n"),
			file:write(File, "terminate(_Reason, {}) -> ok.\r\n"),
			file:write(File, "code_change(_OldVsn, State, _Extra) -> {ok, State}.\r\n"),
			file:write(File, "handle_call(_Request, _From, State) ->  {reply, ok, State}.\r\n"),
			file:write(File, "handle_info(_Request, State) -> {noreply, State}.\r\n"),
			file:write(File, "handle_cast(stop, State) -> {stop, normal, State};\r\n"),
			file:write(File, "handle_cast(_Request, State) -> {noreply, State}.\r\n"),			
			file:close(File);
		_ -> skip
	end.
create_super_file(Module,ThisSuperName,ThisSuperMoudle) ->
	DataDes = Module:get_des(ThisSuperName),
	case create_sup_file(ThisSuperName,ThisSuperMoudle,DataDes) of
		{WorkList,SupList} ->
			FunMakeWorker = fun({WorkerName,WorkerMoudle}) -> create_worker_file(Module,WorkerName,WorkerMoudle) end,
			lists:foreach(FunMakeWorker, WorkList),
			FunMakeSuper = fun({SuperName,SuperMoudle}) -> create_super_file(Module,SuperName,SuperMoudle) end,
			lists:foreach(FunMakeSuper, SupList);
		_ -> ok
	end.